import { ref } from 'vue'
import { SubmitForm } from '@/components/SubmitForm'

export default function dialogHooks(emit: any) {
  const dialogVisible = ref<boolean>(false)
  const submitForm = ref<any>({})
  const submitFormRef = ref<InstanceType<typeof SubmitForm>>()
  const submitFormSchema = ref<SubmitFormSchema[]>([])

  const handleClose = (flag: boolean, callback?: () => void) => {
    if (flag) {
      emit('close')
    }
    submitForm.value = {}
    submitFormRef.value?.resetFields()
    callback?.()
    dialogVisible.value = false
  }
  const handleCheckFormRules = async (callback?: () => void) => {
    try {
      await submitFormRef.value?.validateForm()
      console.log('通过数据格式校验')
      callback?.()
    } catch (error) {
      console.log('未通过数据格式校验' + error)
    }
  }
  return {
    submitForm,
    submitFormRef,
    submitFormSchema,
    dialogVisible,
    handleCheckFormRules,
    handleClose
  }
}
